package com.turing.pre.processor.file.processor;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;


public class TextParser {

    private static TextParser parser = null;
    private TextParser(){}
    public static synchronized TextParser getInstance(){
        if(parser == null){
            parser = new TextParser();
        }
        return parser;
    }
    public void text2pdf(String text,String pdf) throws DocumentException, IOException {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
        FileOutputStream out = new FileOutputStream(pdf);
        Rectangle rect = new Rectangle(PageSize.A4.rotate());
        Document doc = new Document(rect);
        PdfWriter writer = PdfWriter.getInstance(doc, out);
        doc.open();
        Paragraph p = new Paragraph();
        p.setFont(FontChinese);
        BufferedReader read = new BufferedReader(new FileReader(text));
        String line = read.readLine();
        while(line != null){
            System.out.println(line);
            p.add(line+"\n");
            line = read.readLine();
        }
        read.close();
        doc.add(p);
        doc.close();
    }
    public static void main(String[] args) throws Exception {
        java.io.File file = new java.io.File(TextParser.class.getResource("/").getPath());
        String path = file.getParentFile().getPath();
        path = java.net.URLDecoder.decode(path,"UTF-8");
        String text = "D:\\tmp\\dubbo.txt";
        String pdf = "D:\\tmp\\dubbo.pdf";
        TextParser p = TextParser.getInstance();
        p.text2pdf(text, pdf);
    }

}
